package com.jr9z.jrjz.service.impl;

import com.jr9z.jrjz.dao.MinistryCaseMapper;
import com.jr9z.jrjz.entity.MinistryCase;
import com.jr9z.jrjz.service.MinistryCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:Mrzhang
 * @Description:
 * @Date:2019.03.25 21:54
 **/

@Service
public class MinistryCaseServiceImpl implements MinistryCaseService {

    @Autowired
    private MinistryCaseMapper ministryCaseMapper;

    @Override
    public int deleteByPrimaryKey(Integer caseId) {
        return ministryCaseMapper.deleteByPrimaryKey(caseId);
    }

    @Override
    public int insert(MinistryCase record) {
        return ministryCaseMapper.insert(record);
    }

    @Override
    public int insertSelective(MinistryCase record) {

        Integer sort = ministryCaseMapper.selectMaxSort();

        //如果为0，目前还没有数据
        if (sort == 0) {
            //将sort值设为1
            record.setCaseSort(1);
        } else {
            //否则在最大的sort值上加一
            record.setCaseSort(sort + 1);
        }

        return ministryCaseMapper.insertSelective(record);
    }

    @Override
    public MinistryCase selectByPrimaryKey(Integer caseId) {
        return ministryCaseMapper.selectByPrimaryKey(caseId);
    }

    @Override
    public int updateByPrimaryKeySelective(MinistryCase record) {
        return ministryCaseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(MinistryCase record) {
        return ministryCaseMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(MinistryCase record) {
        return ministryCaseMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<MinistryCase> getMinistryCaseByServiceId(Integer serviceId) {
        return ministryCaseMapper.getMinistryCaseByServiceId(serviceId);
    }

    @Override
    public Long getServiceCaseByServiceId(Integer serviceId) {
        return ministryCaseMapper.getServiceCaseByServiceId(serviceId);
    }

    @Override
    public void moveUp(Integer serviceId) {
        //获取要上移的那条数据的信息
        MinistryCase ministryCase = ministryCaseMapper.selectByPrimaryKey(serviceId);

        //查询上一条记录
        MinistryCase casePrev = ministryCaseMapper.moveUp(ministryCase.getCaseSort());

        //最上面的记录不能上移
        if (casePrev == null) {
            return;
        }
        //交换两条记录的sort值
        Integer temp = ministryCase.getCaseSort();
        ministryCase.setCaseSort(casePrev.getCaseSort());
        casePrev.setCaseSort(temp);

        //更新到数据库
        ministryCaseMapper.updateByPrimaryKeySelective(ministryCase);
        ministryCaseMapper.updateByPrimaryKeySelective(casePrev);
    }

    @Override
    public void moveDown(Integer serviceId) {
        //获取要下移的那条数据的信息
        MinistryCase ministryCase = ministryCaseMapper.selectByPrimaryKey(serviceId);

        //查询下一条记录
        MinistryCase caseNext = ministryCaseMapper.moveDown(ministryCase.getCaseSort());

        //最下面的记录不能下移
        if (caseNext == null) {
            return;
        }
        //交换两条记录的sort值
        Integer temp = ministryCase.getCaseSort();
        ministryCase.setCaseSort(caseNext.getCaseSort());
        caseNext.setCaseSort(temp);

        //更新到数据库
        ministryCaseMapper.updateByPrimaryKeySelective(ministryCase);
        ministryCaseMapper.updateByPrimaryKeySelective(caseNext);
    }
}
